//
//  HMVisitorTableViewController.swift
//  Weibo12
//
//  Created by HM on 15/12/28.
//  Copyright © 2015年 itheima. All rights reserved.
//  访客视图的基类：如果发现用户没有登录，就显示没有登录的页面，否则不做任何事情＝

import UIKit

class HMVisitorTableViewController: UITableViewController {

    // 代表当前是否登录
    var userLogin: Bool = HMUserAccountViewModel.sharedViewModel.userLogin
    
    // 加载视图，给 view 设置值
    override func loadView() {
        // 根据用户是否登录给 view 做不同的逻辑
        userLogin == true ? super.loadView() : setupBaseUI()
    }
    
    private func setupBaseUI(){
        // 当前没有用户登录
        // 初始化没有登录的 view
        // 设置到当前控件器上
        view = visitorView
        
        // 设置左右的 item
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", target: self, action: "registerButtonClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", target: self, action: "loginButtonClick")
        
        visitorView.registerButton.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        visitorView.loginButton.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    // MARK: - HMVisitorViewDelegate
    
    @objc private func registerButtonClick() {
        let vc = HMNavigationController(rootViewController: HMOAuthViewController())
        presentViewController(vc, animated: true, completion: nil)
    }
    
    @objc private func loginButtonClick() {
        let vc = HMNavigationController(rootViewController: HMOAuthViewController())
        presentViewController(vc, animated: true, completion: nil)
    }
    
    // MARK: - 懒加载控件
    lazy var visitorView: HMVisitorView = HMVisitorView()
}
